package com.qtyx.nms.ui.fragment

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.ldd.base.ui.fragment.BaseLddFragment
import com.qtyx.nms.R
import com.qtyx.nms.mvp.model.bean.MessageBean
import com.qtyx.nms.ui.activity.HomePageActivity
import com.qtyx.nms.ui.activity.MessageListActivity
import com.qtyx.nms.ui.adapter.HomePageMessageAdapter
import com.qtyx.nms.util.IntentKey
import kotlinx.android.synthetic.main.fg_message.*
import java.util.*

/**
 * 消息中心
 */
class MessageFragment : BaseLddFragment() {
    override fun getLayoutId() = R.layout.fg_message
    override fun initData() {
    }

    var adapter: HomePageMessageAdapter?=null
    var listMessage= mutableListOf<MessageBean.Data>()

    fun setData(list: List<MessageBean.Data>) {
        if (list.isNotEmpty()) {
            listMessage.clear()
            listMessage.addAll(list)
            rvHomepageMessage.visibility = View.VISIBLE
            tvMessageEmptyHint.visibility = View.GONE
            rvHomepageMessage.layoutManager = LinearLayoutManager(mContext)
            if(null==adapter){
                adapter=HomePageMessageAdapter(mContext, listMessage)
                rvHomepageMessage.adapter = adapter
                //点击需要判断是否进入消息列表返回时候更新消息数据
                adapter?.onItemClickListener=object:HomePageMessageAdapter.OnItemClickListener{
                    override fun onItemClick(position: Int, data: MessageBean.Data) {
                        HomePageActivity.isEnterMessageList=true
                        val bundle= Bundle()
                        bundle.putParcelableArrayList(IntentKey.messageList, listMessage as ArrayList)
                        bundle.putInt(IntentKey.messageSelectPosition,position)
                        startActivityCustom(MessageListActivity::class.java,bundle)
                    }
                }
            }else{
                adapter?.notifyDataSetChanged()
            }

        } else {
            rvHomepageMessage.visibility = View.GONE
            tvMessageEmptyHint.visibility = View.VISIBLE
        }
    }

}